﻿/* Copyright (c) 2015 Slava Naumenko - http://m4w.codeplex.com */

using Microsoft.SharePoint;
using System;
using System.IO;
using System.Web;

namespace M4W.ASM.Services
{
    internal static class Helper
    {
        // Get Current Url

        internal static string GetCurrentUrl()
        {
            string curl = "";

            if (HttpContext.Current.Request.UrlReferrer != null)
            {
                curl = HttpContext.Current.Request.UrlReferrer.ToString();
            }
            else
            {
                curl = SPContext.Current.Web.Url.ToString();
            }

            return curl;
        }

        // Get List

        internal static SPList GetList(SPWeb web, String listText)
        {
            SPList listRef = null;

            try
            {
                listRef = web.Lists[listText];
            }
            catch (ArgumentException) { }

            try
            {
                listRef = web.Lists[new Guid(listText)];
            }
            catch (FormatException) { }
            catch (ArgumentException) { }

            try
            {
                if (listRef == null)
                {
                    listRef = web.GetList(listText);
                }
            }
            catch (FileNotFoundException) { }

            try
            {
                if (listRef == null)
                {
                    String webUrl;
                    String listRelativeUrl;

                    listRelativeUrl = web.Site.RootWeb.GetServerRelativeUrlFromUrl(listText);

                    if (listRelativeUrl.ToLower().Contains("/lists/"))
                    {
                        webUrl = listRelativeUrl.Substring(0, listRelativeUrl.ToLower().LastIndexOf("/lists/"));
                    }
                    else
                    {
                        webUrl = listRelativeUrl.Substring(0, listRelativeUrl.LastIndexOf("/"));
                    }

                    using (SPWeb listWeb = web.Site.OpenWeb(webUrl))
                    {
                        listRef = listWeb.GetList(listRelativeUrl);
                    }
                }

            }
            catch (FileNotFoundException) { }

            return listRef;
        }

        // Set List Query

        internal static void SetListQuery(SPQuery query, string Query)
        {
            string viewFields = "";
            int vpos = Query.ToLower().IndexOf("<viewfields>");
            if (vpos != -1)
            {
                viewFields = Query.Substring(vpos);
                viewFields = viewFields.Substring(0, viewFields.ToLower().IndexOf("</viewfields>") + 13);

                query.Query = Query.Replace(viewFields, "");

                viewFields = viewFields.Substring(12);
                viewFields = viewFields.Substring(0, viewFields.ToLower().IndexOf("</viewfields>"));

                query.ViewFields = viewFields;
                query.ViewFieldsOnly = true;
            }
            else
            {
                query.Query = Query;
            }
        }

        // List Field

        internal static bool ListFieldExists(SPList List, string Field)
        {
            bool fieldExists = false;

            try
            {
                SPField columnField = List.Fields.GetFieldByInternalName(Field);
                fieldExists = columnField != null;
            }
            catch
            {
                fieldExists = false;
            }

            return fieldExists;
        }

        // Verify File

        internal static bool VerifyFile(string filepath)
        {
            bool result = false;

            FileInfo file = new FileInfo(filepath);
            long size = file.Length;

            if (size > 0)
            {
                result = true;
            }

            return result;
        }

        // Get Bytes

        internal static Byte[] GetBytes(string str)
        {
            Byte[] bytes = new byte[str.Length * sizeof(char)];
            System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
            return bytes;
        }
    }

    // Handle Event Firing

    internal class HandleEventFiring : SPItemEventReceiver
    {
        internal void DisableHandleEventFiring()
        {
            this.EventFiringEnabled = false;
        }

        internal void EnableHandleEventFiring()
        {
            this.EventFiringEnabled = true;
        }
    }
}